package com.app.micaihu.view.army.activity

import com.app.base.BaseMvpActivity
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.configure.Config
import com.app.micaihu.configure.LoadType
import com.app.micaihu.view.army.adapter.ArmyMessageListAdapter
import com.app.micaihu.view.army.contract.ArmyMessageListContract
import com.app.micaihu.view.army.presenter.ArmyMessageListPresenter
import com.app.micaihu.view.bean.ArmyMessage
import com.app.micaihu.view.main.view.WrapContentLinearLayoutManager
import com.app.utils.biz.Biz
import com.blankj.utilcode.util.ToastUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.army_message_list_activity.*
import kotlinx.android.synthetic.main.army_message_list_activity.refreshLayout
import kotlinx.android.synthetic.main.army_message_list_activity.rvList
import kotlinx.android.synthetic.main.view_common_rv.*

/**
 * 军团消息列表
 */
class ArmyMessageListActivity : BaseMvpActivity<ArmyMessageListContract.View, ArmyMessageListPresenter>(), ArmyMessageListContract.View {

    override fun getLayoutId() = R.layout.army_message_list_activity

    override fun getPresenter() = ArmyMessageListPresenter()

    private var auditingType = 0 //审批类型 1同意 2拒绝
    private var auditingIndex = 0 //审批 下标
    private val armyMessageListAdapter: ArmyMessageListAdapter by lazy {
        val armyMessageListAdapter = ArmyMessageListAdapter()
        rvList.layoutManager = WrapContentLinearLayoutManager(thisContext)
        rvList.adapter = armyMessageListAdapter
        armyMessageListAdapter
    }

    override fun setData() {
        refreshLayout.setEnableLoadMore(true)
        refreshLayout.setEnableAutoLoadMore(true)
        mPresenter?.refresh()
    }

    override fun addListeners() {
        refreshLayout.setOnRefreshListener {
            mPresenter?.refresh()
        }
        refreshLayout.setOnLoadMoreListener {
            mPresenter?.loadMore()
        }
        armyMessageListAdapter.addChildClickViewIds(R.id.tvReject, R.id.tvAgree, R.id.tvInviteReject, R.id.tvInviteAgree)
        armyMessageListAdapter.setOnItemChildClickListener { adapter, view, position ->
            val item = armyMessageListAdapter.getItem(position)
            auditingIndex = position
            when (view.id) {
                R.id.tvReject -> {
                    auditingType = 2
                    mPresenter?.armyAuditing(item.gid, item.to_uid, auditingType)
                }
                R.id.tvAgree -> {
                    auditingType = 1
                    mPresenter?.armyAuditing(item.gid, item.to_uid, auditingType)
                }
                R.id.tvInviteReject -> {
                    auditingType = 2
                    mPresenter?.armyAuditing(item.gid, Biz.instance.uid(), auditingType)
                }
                R.id.tvInviteAgree -> {
                    auditingType = 1
                    mPresenter?.armyAuditing(item.gid, Biz.instance.uid(), auditingType)
                }
            }
        }
    }


    override fun handleArmyMessageList(result: BaseBean<List<ArmyMessage>?>?, loadType: Int) {
        refreshLayout.finishRefresh(Config.delayRefresh)
        if (result == null) {
            if (armyMessageListAdapter.data.isNotEmpty()) {
                if (LoadType.TYPE_REFRESH_ERROR == loadType) {
                    ToastUtils.showShort(R.string.empty_network_error)
                } else {
                    refreshLayout.finishLoadMore(false)
                }
            } else {
                if (LoadType.TYPE_REFRESH_ERROR == loadType) {
                    armyMessageListAdapter.setEmptyView(errorView)
                } else {
                    armyMessageListAdapter.setEmptyView(getEmptyView())
                }
            }

        } else {
            if (result.isSuccess()) {
                result.data?.let {
                    if (LoadType.TYPE_REFRESH_SUCCESS == loadType) {
                        armyMessageListAdapter.setList(it)
                    } else if (LoadType.TYPE_LOAD_MORE_SUCCESS == loadType) {
                        armyMessageListAdapter.addData(it)
                    }
                    if (it.isNullOrEmpty()) {
                        armyMessageListAdapter.setEmptyView(getEmptyView())
                        refreshLayout.finishLoadMoreWithNoMoreData()
                    } else {
                        refreshLayout.finishLoadMore(Config.delayLoadMore)
                    }
                }

            } else {
                ToastUtils.showShort(result.msg)
            }
        }

    }

    override fun handleArmyAuditing(result: BaseBean<Any?>?) {
        if (result == null) {
            ToastUtils.showShort(getString(R.string.net_error))
        } else {
            if (result.isSuccess()) {
                armyMessageListAdapter.getItem(auditingIndex).status = auditingType
                armyMessageListAdapter.notifyItemChanged(auditingIndex)
            }
            ToastUtils.showShort(result.msg)
        }
    }
}